﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui.Parametrage
{
    /// <summary>
    /// Interaction logic for W_Parametrage.xaml
    /// </summary>
    public partial class W_Parametrage : Window
    {
        HotelClient srvService;
        public W_Parametrage()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            busyTool.Width = SystemParameters.WorkArea.Width;
            busyTool.Height = SystemParameters.WorkArea.Height;

            gridPrincipal.Width = SystemParameters.WorkArea.Width;
            gridPrincipal.Height = SystemParameters.WorkArea.Height - 25;

            srvService = new HotelClient();
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        private void bt_ajoutStatutChambre_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Ref_Statut_Chambres rsch = new Ref_Statut_Chambres();

                rsch.Nom_Statut_Chambre = tb_nomStatutChamb.Text.Trim();
                rsch.Description_Statut_Chambre = tb_descStatutChamb.Text.Trim();
                //srvService = new HotelClient();
                if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                {
                    //writeToBusyTool("Connection au serveur...");
                    srvService.Open();
                }
                int resultatAjout = srvService.AddRefStatutChambres(rsch);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("Enregistrement réussi",
                                    "Ajout Statut Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("Erreur d'enresgistrement",
                                    "Ajout Statut Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Statut Chambre", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void bt_ajoutTypeChambre_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Ref_Type_Chambres rtch = new Ref_Type_Chambres();

                rtch.Nom_Type_Chambre = tb_nomType.Text.Trim();
                if (tb_tarifTypeCh.Text.Trim().Length > 0)
                {
                    rtch.Tarif_Standard_Chambre =Convert.ToDecimal(tb_tarifTypeCh.Text.Trim());    
                }                
                rtch.Description_Type_Chambre = tb_descTypeCh.Text.Trim();
                if (tb_nbLitTypeCh.Text.Trim().Length > 0)
                {
                    rtch.Nombre_lit = Convert.ToInt16(tb_nbLitTypeCh.Text.Trim());  
                }
                if (cb_fumeurTypeCh.SelectedValue!=null)
                {
                    if (cb_fumeurTypeCh.SelectedValue.ToString() == "1")
                    {
                        rtch.fumeur = true; 
                    }
                    else
                        rtch.fumeur = false;   
                }
                

                //srvService = new HotelClient();
                if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                {
                    //writeToBusyTool("Connection au serveur...");
                    srvService.Open();
                }
                int resultatAjout = srvService.AddRefTypeChambres(rtch);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("Enregistrement réussi",
                                    "Ajout Type Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("Erreur d'enresgistrement",
                                    "Ajout Type Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Type Chambre", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void image1_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        #region Ref Caractéristiques Hotels
        
        private void bt_addRefCarctHotel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Ref_Caracteristique_Hotels rch = new Ref_Caracteristique_Hotels();

                rch.Code_Caracteristique = tb_codeCaracHotel.Text.Trim();
                rch.Description_Caracterisque = tb_descCaractHotel.Text.Trim();
                //srvService = new HotelClient();
                if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                {
                    //writeToBusyTool("Connection au serveur...");
                    srvService.Open();
                }
                int resultatAjout = srvService.AddRefCaracteristiqueHotels(rch);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("Enregistrement réussi",
                                    "Ajout Caractéristique Hotel",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeRefCaractHotel();
                }
                else
                {
                    MessageBox.Show("Erreur d'enresgistrement",
                                    "Ajout Caractéristique Hotel",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Caractéristique Hotel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //chargement des ref caractéristiques hotels
        private void loadListeRefCaractHotel()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des caractéristiques d'hotel...");
                    //srvService = new HotelClient();
                    if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    {
                        writeToBusyTool("Connection au serveur...");
                        srvService.Open();
                    }
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_refCaractHotel.DataContext = srvService.getListRefCaracteristiqueHotels();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Caractéristique Hotel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        #endregion
    }
}
